<?php

namespace App\Jobs;

use App\Models\Api\CompanyContact;
use App\Models\Common\CollOption;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyDepartment;
use App\Models\Company\CompanyUser;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Facades\Site;
use DB, Mail, Log;

//引入第三方微信文件
require_once(app_path('Vendor/MpWeiXin/MpWeiXin.php'));

class CompanyResumePush extends Job implements ShouldQueue{
    use InteractsWithQueue, SerializesModels;
    protected $resumeData;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($resumeData){
        $this->resumeData = $resumeData;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(){
        $this->config();
        $companyUser = DB::table('company_users as t1')
            ->join('company_job as t2', 't1.uid', '=', 't2.uid')
            ->select('t1.wechatmpopenid', 't2.contact_info', 't2.job_title', 't1.viptime', 't1.device_token')
            ->where('t2.id', $this->resumeData->job_id)
            ->where('t1.uid', $this->resumeData->company_uid)
            ->first();
        //获取用户基本信息
        $resume = DB::table('resume as t1')
            ->leftjoin('resume_education_exp as t2', function($join){
                $join->on('t1.id', '=', 't2.resume_id')
                    ->where('t2.edu_type','=', 1);
            })

            ->where('t1.uid', $this->resumeData->personal_uid)
            ->where('t1.id', $this->resumeData->resume_id)
            ->select('t1.name', 't1.mobile', 't1.work_year','t1.intentionjobs','t1.expectedsalary','t2.education', 't2.school')
            ->orderBy('t2.education', 'desc')
            ->first();
        //获取联系方式
        if(!empty($companyUser)){
            switch($companyUser->contact_info){
                case  0 :
                    $contact = CompanyBasic::where('id', $this->resumeData->com_id)->where('uid', $this->resumeData->company_uid)
                        ->first(['contacts', 'mobile', 'phone', 'email']);
                    break;
                case  1 :
                    $contact = CompanyDepartment::where('uid', $this->resumeData->company_uid)->where('com_id', $this->resumeData->com_id)
                        ->first(['contacts', 'mobile', 'phone', 'email']);
                    break;
                case  2 :
                    $contact = CompanyContact::where('uid', $this->resumeData->company_uid)->where('job_id', $this->resumeData->job_id)
                        ->first(['contacts', 'mobile', 'phone', 'email']);
                    break;
                case  3 :
                    $contact = CompanyBasic::where('id', $this->resumeData->com_id)->where('uid', $this->resumeData->company_uid)
                        ->first(['contacts', 'mobile', 'phone', 'email']);
                    break;
            }
        }
        if(!empty($resume)){
            $resume->send_id = $this->resumeData->id;
            $resume->resume_id = $this->resumeData->resume_id;
            $resume->job_title = $companyUser->job_title;
            $resume->education = CollOption::getOptionName($resume->education, 'education');
            $resume->contact = !empty($contact->contacts) ? $contact->contacts :'';
            $resume->phone = !empty($contact->mobile) ? $contact->mobile : $contact->phone;
            $resume->school = !empty($resume->school) ? $resume->school : '';
            $resume->work_year = CollOption::getOptionName($resume->work_year, 'work_year');
            //判断该企业是不是会员
            $curr_time = date('Y-m-d H:i:s', time());
            if(!empty($companyUser->viptime) && $curr_time > $companyUser->viptime){
                $resume->mobile = '请先注册会员才能查看联系方式';
            }else if(empty($companyUser->viptime)){
                $resume->mobile = '请先注册会员才能查看联系方式';
            }
            $resume->email =!empty($contact->email) ? $contact->email :'';
        }

        //检查device_token,app_key,master_secret是否存在
        if(!empty($companyUser->device_token) && !empty(Site::get('company_app_key')) &&
            !empty(Site::get('company_master_secret'))) {
            //发送远程推送
            $job = (new CompanyResumeApiPush($companyUser, $resume));
            dispatch($job);
        }
        if(!empty($companyUser->wechatmpopenid)){//检查微信是不是绑定
            //微信企业收到模板消息直接查看简历生成自动登录地址
            $resumeUrl = mainUrl('mobile/resume_' . idEncryption($this->resumeData->resume_id)) . '.html';
            if (!empty($this->resumeData->company_uid)) {
                $companyUser = CompanyUser::where('uid', '=', $this->resumeData->company_uid)->first();
                if (!empty($companyUser->wechatmpopenid)) {
                    $strPassword = substr($companyUser->password, 0 ,16);//取密码前16
                    $strEncryption = md5($strPassword . date('Y-W'));//拼接当前周数md5加密
                    $redirectType = 0;//验证没通过是否重定向，1是0否
                    $resumeUrl .= "/?wechatmpopenid=$companyUser->wechatmpopenid&str=$strEncryption&redirect=$redirectType";
                }
            }
            $weixin = new \MpWeiXin();
            $weixin->companyMessagePush($companyUser->wechatmpopenid, $resume, $resumeUrl);
            //检查邮箱是不是存在
        }elseif(!empty($contact->email)){
            if(!empty(Site::get('MailAddress')) && !empty($contact->email)){
                Mail::send('emails.company_resume_email', ['resume' => $resume], function($m) use ($contact,$resume){
                    $m->from(Site::get('MailAddress'), Site::get('webname'));
                    $m->to($contact->email)->subject(Site::get('webname').'-'.$resume->name.'申请贵公司'.$resume->job_title);
                });
            }else{
                Log::info('发送邮件失败,e-mail:'.$contact->email);
            }
            //检查手机号码是不是存在
            if(!empty($contact->mobile) && strlen($contact->mobile) == '11'){
                //检查投递记录是不是被查看
                if($this->resumeData->status == 0){
                    //延迟半天推送
                    $job = (new CompanyResumeSmsPush($resume))->delay(12 * 60 * 60);
                    dispatch($job);
                }
            }
        }else{
            //检查手机号码是不是存在
            if(!empty($contact->mobile) && strlen($contact->mobile) == '11'){
                //检查投递记录是不是被查看
                if($this->resumeData->status == 0){
                    //延迟半天推送
                    $job = (new CompanyResumeSmsPush($resume))->delay(12 * 60 * 60);
                    dispatch($job);
                }
            }
        }
    }
}
